package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseOriginal;
import com.isunland.managesystem.base.BaseTableListAdapter;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.rOutappMain;
import com.isunland.managesystem.entity.rOutappSub;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/30 10:52
 */
public class RetailOutMaterielListFragment extends BaseListFragment {
  public static final int ADD_MATERIEL = 0;
  public static final int DETAIL_MATERIEL = 1;
  private BaseTableListAdapter mAdapter;
  private ArrayList<rOutappSub> mlist;
  private static rOutappMain outMain;
  private static String mainId;
  private static int mType;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    outMain = (rOutappMain) mBaseParams.getItem();
    mainId = outMain.getId();
    mType = mBaseParams.getType();
    mlist = new ArrayList<>();
  }

  @Override public String getUrl() {
    return ApiConst.URL_OUT_SUB_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("mainId", outMain.getId());
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    BaseOriginal<rOutappSub> original =
        new Gson().fromJson(json, new TypeToken<BaseOriginal<rOutappSub>>() {
        }.getType());
    if (Base.SUCCESS == original.getResult() && original.getRows() != null) {
      mlist.clear();
      mlist.addAll(original.getRows());
      LinkedHashMap<String, String> map = new LinkedHashMap<>();
      map.put("物料名称", "materialName");
      map.put("申请数量", "outappNumber");
      map.put("单位", "munit");
      if (mAdapter == null) {
        mAdapter = new BaseTableListAdapter<>(getActivity(), mlist, map, new rOutappSub());
      }
      setListAdapter(mAdapter);
      ((BaseTableListAdapter) getListAdapter()).notifyDataSetChanged();
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  //点击跳转到物料详情界面
  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    rOutappSub outSub = mlist.get(position - 1);
    Intent i = new Intent(getActivity(), RetailOutMaterieDetailActivity.class);
    i.putExtra(RetailOutMaterieDetailFragment.EXTRA_MAIN_CONTENT, outMain);
    i.putExtra(RetailOutMaterieDetailFragment.EXTRA_SUB_CONTENT, outSub);
    i.putExtra(RetailOutMaterieDetailFragment.EXTRA_RESOURCEFLAG, mBaseParams.getRemark());
    startActivityForResult(i, DETAIL_MATERIEL);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if ("new".equals(outMain.getMoutbillStatus()) || "abort".equals(outMain.getMoutbillStatus())) {
      inflater.inflate(R.menu.menu_add, menu);
    }
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_add:
        if (BaseParams.TYPE_ADD == mType) {
          ToastUtil.show("请先保存主表");
        } else {
          Intent i = new Intent(getActivity(), RetailOutMaterieDetailActivity.class);
          i.putExtra(RetailOutMaterieDetailFragment.EXTRA_MAIN_CONTENT, outMain);
          i.putExtra(RetailOutMaterieDetailFragment.EXTRA_RESOURCEFLAG, mBaseParams.getRemark());
          startActivityForResult(i, ADD_MATERIEL);
        }
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == ADD_MATERIEL) {
      volleyPost();
    }
    if (requestCode == DETAIL_MATERIEL) {
      volleyPost();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static void getContent(rOutappMain main, int type) {
    outMain = main;
    mType = type;
    mainId = outMain.getId();
    LogUtil.i("更新后mContent  id=====" + mainId);
  }
}
